const curl = require('../src/curl');
const dateUtils = require('date-utils');
const qxArea = require('./qx_area');

const area_array = qxArea.area.split(',');
const area_list = area_array.map((item, index) => {
  let item_array = item.split("|");
  let name = item_array[0];
  let code = item_array[1];

  let type = "province";
  /^\d{2}0{4}$/.test(code) ? type = "province" : "0" === code ? type = "country" : type = "city";

  return {
    index: index,
    name: name,
    id: code,
    dt: type
  }
});

const move_type = ['move_in', 'move_out'];

const resolveData = function (data) {
  let cb = function (data) {
    if (data.errmsg === 'SUCCESS') {
      return data.data.list;
    } else {
      return [];
    }
  }

  if (data !== null) {
    try {
      data_list = eval("(" + data + ")");
      return data_list;
    } catch {
      return [];
    }
  } else {
    return [];
  }
}

const getCityrank_async = function (dt, id, type, date, callback) {
  let url = `https://huiyan.baidu.com/migration/cityrank.jsonp?dt=${dt}&id=${id}&type=${type}&date=${date}`;
  curl.download(url, function (res) {
    let data = resolveData(res);
    callback(data);
  });
}

const getProvincerank_async = function (dt, id, type, date, callback) {
  let url = `https://huiyan.baidu.com/migration/provincerank.jsonp?dt=${dt}&id=${id}&type=${type}&date=${date}`;
  curl.download(url, function (res) {
    let data = resolveData(res);
    callback(data);
  });
}

const getCityrank = function (dt, id, type, date) {
  let url = `https://huiyan.baidu.com/migration/cityrank.jsonp?dt=${dt}&id=${id}&type=${type}&date=${date}`;
  let data = curl.syncRequest('GET', url);
  return resolveData(data);
}

const getProvincerank = function (dt, id, type, date) {
  let url = `https://huiyan.baidu.com/migration/provincerank.jsonp?dt=${dt}&id=${id}&type=${type}&date=${date}`;
  let data = curl.syncRequest('GET', url);
  return resolveData(data);
}

const getRank = function (dt, id, type, date, callback) {
  return callback(dt, id, type, date);
}

const getHistorycurve = function (dt, id, type, startDate, endDate) {
  let url = `https://huiyan.baidu.com/migration/historycurve.jsonp?dt=${dt}&id=${id}&type=${type}&startDate=${startDate}&endDate=${endDate}`;
  let data = curl.syncRequest('GET', url);
  return resolveData(data);
}

const getDays = function (startDate) {
  let dateList = [];
  dateList.push(startDate.toYMD(''));
  let diffDays = startDate.getDaysBetween(Date.yesterday());
  for (let i = 0; i <= diffDays; i++) {
    let date = startDate.add({
      "days": 1
    }).toYMD('');

    dateList.push(date);
  }
  return dateList;
}

const getToday = function () {
  return Date.today().toYMD('');
}

const getYestoday = function () {
  return Date.yesterday().toYMD('');
}

exports.areaList = area_list;
exports.move_type = move_type;

exports.getCityrank = getCityrank;
exports.getProvincerank = getProvincerank;
exports.getRank = getRank;

exports.getCityrank_async = getCityrank_async;
exports.getProvincerank_async = getProvincerank_async;

exports.getHistorycurve = getHistorycurve;

exports.getDays = getDays;

exports.getToday = getToday;
exports.getYestoday = getYestoday;